# coding=utf-8
"""
    @project: 15python_spider
    @Author：frank
    @file： 02_maoyan_film_csv.py
    @date：2023/12/26 20:32
"""

from urllib import request
import time
import random
import re
import csv
import requests


class MaoyanSpider(object):
    def __init__(self):
        self.url = 'https://www.maoyan.com/board/4?offset={}'
        self.ua_list = [
            # 百度
            "Mozilla/5.0 (compatible; Baiduspider/2.0;+http://www.baidu.com/search/spider.html) safari 5.1 – MAC",
            # safari 5.1 – MAC
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15",
            # safari 5.1 – Windows
            "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
            # IE 11.0
            "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
            # IE 9.0
            "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0",
            # IE 8.0
            "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
            # IE 7.0
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
            # IE 6.0
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
            # Firefox 65.0 – MAC
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:65.0) Gecko/20100101 Firefox/65.0",
            # Firefox 65.0 – Windows
            "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/65.0",
            # Firefox win7
            "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0",
            # Opera 11.11 – MAC   (欧朋浏览器可以在Windows、Mac和Linux三个操作系统平台上运行)
            "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
            # Opera 11.11 – Windows
            "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
            # Chrome 73.0 – MAC
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36",
            # Chrome Win7
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1",
            # Chrome Win10
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
            # WinXP + IE8
            "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.0)",
            # WinXP + IE7
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
            # WinXP + IE6
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
            # 傲游（Maxthon）
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
            # (腾讯)QQ浏览器
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
            # 世界之窗（The World） 2.x
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
            # 世界之窗（The World） 3.x
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
            # 搜狗浏览器 1.x
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
            # 360浏览器
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
            # Avant
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
            # Green Browser
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
        ]

    # 获取数据
    def get_page(self, url):
        # 每次使用随机的user-agent
        headers = {
            'User-Agent': random.choice(self.ua_list)
        }
        req = requests.get(url=url, headers=headers)
        req.encoding = 'utf-8'
        html_content = req.text
        # req = request.Request(url=url, headers=headers)
        # res = request.urlopen(req)
        # html_content = res.read().decode('utf-8')

        self.parse_page(html_content)

    def parse_page(self, html_content):
        pattern = re.compile(
            r'data-val="{movieId:\d+}">(\w+?)</a></p>.+?<p class="star">(.+?)</p>.+?<p class="releasetime">(.+?)</p>',
            re.S)

        film_list = pattern.findall(html_content)
        self.write_page(film_list)

    def write_page(self, film_list):
        film_list_rows = []
        for film in film_list:
            film_list_rows.append([film[0].strip(), film[1].strip(), film[2].strip()])
        with open('猫眼榜单top100.csv', 'a', encoding="utf-8", newline='') as f:
            writer = csv.writer(f)
            writer.writerows(film_list_rows)

    def main(self):
        for i in range(10):
            url = self.url.format(i * 10)
            self.get_page(url)
            time.sleep(random.randint(1, 3))


if __name__ == "__main__":
    start = time.time()
    spider = MaoyanSpider()
    spider.main()
    end = time.time()
    print('执行时间:%.2f' % (end - start))
